
/*
 * @项目名称: 模板开发
 * @FilePath: /meek/src/renderer/render/ellipserender.js
 * @Author: kongxueyang@datatang.com
 * @Date: 2020-09-16T15:29:54+08:00
 * @LastEditors: kongxueyang kongxueyang@datatang.com
 * @LastEditTime: 2023-09-21 10:34:20
 * Copyright (c) 2018-2022 
 */
                                                    
/**
 * Created by zhangyong on 2017/3/20.
 */
import GeometryRender from '../render/geomertyrender'
import LineStyle from '../../style/linestyle'

import { colorToString } from '../../utils/helpers'
import { Transform } from '../../data/matrix/transform'
import PointStyle from "../../style/pointstyle"
import Circle from './../../geometry/circle';
import { drawCenterPoint } from './circlerender'
import { drawPoint } from './drawPoint'

export default class EllipseRender extends GeometryRender {

  constructor(context) {
    super(context)

    this._pixelCoordinates = []
  }

  /**
   * Render a point
   * @param feature
   * @param transform
   */
  render(feature, renderStyle, transform) {
    if (!feature) {
      return
    }

    const ctx = this.context
    const styleArray = renderStyle
    const geomerty = feature.geometry

    const coordinates = [geomerty.x, geomerty.y];
    const coordinates2 = [geomerty.x + geomerty.radiusX, geomerty.y];
    const coordinates3 = [geomerty.x, geomerty.y + geomerty.radiusY];
    // TODO  Should be cached
    const pixelCoordinates = Transform.transform2D(
      coordinates, 0, coordinates.length, 2,
      transform, this._pixelCoordinates)
    const pixelCoordinates2 = Transform.transform2D(
      coordinates2, 0, coordinates2.length, 2,
      transform, this._pixelCoordinates)
    const pixelCoordinates3 = Transform.transform2D(
      coordinates3, 0, coordinates3.length, 2,
      transform, this._pixelCoordinates)
    const x = pixelCoordinates[0]
    const y = pixelCoordinates[1]
    const len = styleArray.length;

    const vertexs = geomerty.getVertexs().map(item => {
      return Transform.transform2D(item, 0, item.length, 2, transform, this._pixelCoordinates);
    })
    for (let i = 0; i < len; i++) {
      let styleObj = styleArray[i]
      let renderOptions = {
        x: x,
        y: y,
        radiusX: Circle.getDistance(pixelCoordinates, pixelCoordinates2),
        radiusY: Circle.getDistance(pixelCoordinates, pixelCoordinates3),
        rotation: feature.viewRotation ? geomerty.rotation + feature.viewRotation : geomerty.rotation,
        sAngle: geomerty.sAngle,
        eAngle: geomerty.eAngle,
        counterclockwise: geomerty.counterclockwise,
        fillStyle: colorToString(styleObj.color, styleObj.alpha),
        borderStyle: styleObj.borderStyle,
        vertexs
      }
      this.drawCircle(ctx, renderOptions, feature)
      const { radiusX, radiusY } = renderOptions;
      drawPoint(ctx, [vertexs], feature, styleArray);

    }
  }


  drawCircle(ctx, renderOptions, feature) {
    // console.log('类型为圆，开始绘制圆')
    // 开始绘制外层圆
    ctx.save()

    ctx.beginPath()
  
    ctx.ellipse(renderOptions.x, renderOptions.y, renderOptions.radiusX, renderOptions.radiusY, renderOptions.rotation, renderOptions.sAngle, renderOptions.eAngle, renderOptions.counterclockwise)
    const borderStyle = renderOptions.borderStyle
    if (borderStyle) {
      ctx.strokeStyle = colorToString(borderStyle.color, borderStyle.alpha)
      ctx.lineWidth = borderStyle.width
      if (borderStyle.style === LineStyle.DASH) {
        ctx.setLineDash([5, 5])
      } else if (borderStyle.style === LineStyle.DASHDOT) {
        ctx.setLineDash([10, 3, 3, 3]);
      }
      ctx.stroke()
    }
    if (renderOptions.fillStyle) {
      ctx.fillStyle = renderOptions.fillStyle
      ctx.fill()
    }
    ctx.closePath()
    ctx.restore()
    if (feature.centralPoint || feature.showCentralLine) {
      drawCenterPoint(ctx, [renderOptions.x, renderOptions.y], renderOptions);
      this.drawLine(ctx, renderOptions);
    }
    
  }

  drawLine(ctx, renderOptions) {
    ctx.save()
    const vertexs = renderOptions.vertexs;
    const borderStyle = renderOptions.borderStyle
    if (borderStyle) {
      ctx.strokeStyle = colorToString(borderStyle.color, borderStyle.alpha)
      ctx.lineWidth = borderStyle.width
      ctx.setLineDash([5, 5])
    }
    ctx.beginPath();
    ctx.moveTo(...vertexs[0]);
    ctx.lineTo(...vertexs[2]);
    ctx.moveTo(...vertexs[1]);
    ctx.lineTo(...vertexs[3]);
    ctx.stroke();
    ctx.restore();
  }
}
